package com.ruitu.mall_98_1.product.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.base.BaseFragment;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.utils.ScreenUtils;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.dao.P_Global;
import com.ruitu.mall_98_1.product.activity.ProductListActivity;
import com.ruitu.mall_98_1.product.activity.ShopDetailActivity;
import com.ruitu.mall_98_1.product.adapter.ShopClassifyAdapter;
import com.ruitu.router_module.bean.product.Product;
import com.ruitu.router_module.bean.product.ShopDetail;
import com.ruitu.router_module.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;

public class ShopDetail1Fragment extends BaseFragment {
    private BGABanner banner_main_cube;
    private RecyclerView rcv_classify;//分类商品列表
    private TextView tv_copyright, tv_phone, tv_addr, tv_support;
    private EditText et_pro_name;// 输入的搜索关键字
    private TextView tv_search;// 搜索

    private ShopClassifyAdapter adapter;
    private BannerAdapter bannerAdapter;

    private ShopDetail shopDetail;

    public static ShopDetail1Fragment newInstance(ShopDetail shopDetail) {
        Bundle args = new Bundle();
        args.putSerializable("shopDetail", shopDetail);
        ShopDetail1Fragment fragment = new ShopDetail1Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shopDetail = (ShopDetail) getArguments().getSerializable("shopDetail");
        adapter = new ShopClassifyAdapter(getActivity());
        bannerAdapter = new BannerAdapter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.pro_fragment_shop_detail_1, container, false);
        banner_main_cube = v.findViewById(R.id.banner_main_cube);
        rcv_classify = v.findViewById(R.id.rcv_classify);
        tv_copyright = v.findViewById(R.id.tv_copyright);
        tv_phone = v.findViewById(R.id.tv_phone);
        tv_addr = v.findViewById(R.id.tv_addr);
        tv_support = v.findViewById(R.id.tv_support);
        et_pro_name = v.findViewById(R.id.et_pro_name);
        tv_search = v.findViewById(R.id.tv_search);

        tv_search.setOnClickListener(this);

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) banner_main_cube.getLayoutParams();
        params.width = ScreenUtils.getScreenWidth();
        params.height = params.width * 375 / 875;//banner的比例是875 * 375
        banner_main_cube.setLayoutParams(params);
        initBanner(shopDetail.getBannerList());

        rcv_classify.setLayoutManager(new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
//        rcv_classify.setLayoutManager(new LinearLayoutManager(getActivity()));
        rcv_classify.setAdapter(adapter);
        adapter.setDataList(shopDetail.getClassifications());

        ((ShopDetailActivity) getActivity()).setShopBottom(tv_copyright, tv_phone, tv_addr, tv_support);

        return v;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_search) {// 点击搜素
            String name = et_pro_name.getText().toString().trim().toUpperCase();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort("请输入商品名称");
                return;
            }

            // 搜索折中方案 并不可取
            List<Product> searchProList = new ArrayList<>();// 用于存储筛选出来的商品
            for (int i = 0; i < shopDetail.getGoodsList().size(); i++) {
                Product pro = shopDetail.getGoodsList().get(i);
                if (pro.getGoodName().toUpperCase().contains(name)) {
                    searchProList.add(pro);// 封装符合关键字的商品到搜索集合中
                }
            }
            P_Global.productList = searchProList;
            startActivity(ProductListActivity.class);
        }
    }

    private void initBanner(List<ShopDetail.Banner> bannerList) {
        banner_main_cube.setAutoPlayAble(true);
        banner_main_cube.setAllowUserScrollable(true);
        banner_main_cube.setAdapter(bannerAdapter);
        List<String> urlList = new ArrayList<>();//图片地址集合
        List<String> introList = new ArrayList<>();//简介集合
        for (ShopDetail.Banner banner : bannerList) {
            urlList.add(banner.getImage());
            introList.add("");//不显示banner简介
        }
        banner_main_cube.setData(urlList, introList);
    }

    //Banner的adapter
    private class BannerAdapter implements BGABanner.Adapter<ImageView, String> {
        @Override
        public void fillBannerItem(BGABanner banner, ImageView itemView, @Nullable String model, int position) {
//            Glide.with(Arad.app).load(model).apply(Arad.opts).into(itemView);
            GlideUtil.loadImg(Config.BASE_URL + model, itemView);
        }
    }
}
